package {
	import org.flixel.FlxSound;
	import org.flixel.FlxObject;
	import org.flixel.FlxPoint;
	import org.flixel.system.FlxAnim;
	import jam.registry.Resource;
	import org.flixel.FlxSprite;

	
	public class Player extends FlxSprite {
		public static const GRAVITY:Number = 200;
		
		public var control:Boolean;
		public var needsToDie:Boolean;
		public var fromLinePile:Boolean;
		
		public var heartRate:Number;
		public const BASE_JUMP_STRENGTH:int = 105;
		public var jumpStrength:Number = BASE_JUMP_STRENGTH;
		public var accelSpeed:Number = 500;
		public var runSpeed:Number = 50;
		public static const MIN_HEART_RATE:Number = 50;
		public static const MAX_HEART_RATE:Number = 200;
		private const STARTING_HEART_RATE:Number = 80;
		
		public static const BUMP_SIZE:Number = 25;
		
		public const decayRate:Number = .04;
		
		public static const LEVEL_ONE_X:Number = 10;
		public static const LEVEL_ONE_Y:Number = 160;
		
		public static const LEVEL_TWO_X:Number = 10;
		public static const LEVEL_TWO_Y:Number = 620;
		
		public static const LEVEL_THREE_X:Number = 10;
		public static const LEVEL_THREE_Y:Number = 230;
		
		public static const LEVEL_FOUR_X:Number = 155;
		public static const LEVEL_FOUR_Y:Number = 10;
		
		private var faceplantSound:FlxSound;
		public var deathCallback:Function;
		
		public function Player(x:Number, y:Number) {
			super(x, y);
			control = true;
			needsToDie = false;
			heartRate = STARTING_HEART_RATE;
//			makeGraphic(10, 12, 0xffaa1111);
			maxVelocity.x = runSpeed;
			// This also controls how fast he can fall, so we should use jumpStrength as the jump modifier
			maxVelocity.y = 500;
			// Not sure what these two do
			acceleration.y = GRAVITY;
			drag.x = 200;
			
			loadGraphic(Resource.PLAYEREXP, true, false, 24, 14);
			addAnimation("idle", [0]);
    		addAnimation("runLeft", [1, 2, 3, 4], 8, true);
    		addAnimation("runRight", [5, 6, 7, 8], 8, true);
			addAnimation("jump", [9]);
			addAnimation("death", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 11, 11, 11, 11, 11, 11, 11 ,11 ,11 ,11, 12, 13, 13, 13, 13, 13, 13, 13, 13], 15, false);
			addAnimationCallback(handleAnimationChanged);

			width = 10;
			height = 14;
			centerOffsets();
			play("idle");
			
			faceplantSound = new FlxSound();
			faceplantSound.loadEmbedded(Resource.SOUND_FACE_PLANT);
		}
	
		public function decay(size:Number = decayRate):void {
			heartRate -= size;
			updateAddiction();
		}
		
		public function comeDown():void {
			heartRate = STARTING_HEART_RATE;
			updateAddiction();
		}
		
		public function takeBump(size:Number):void {
			heartRate += size;
			updateAddiction();
		}
		
		public function updateAddiction():void {
			jumpStrength = BASE_JUMP_STRENGTH+(heartRate/8);
			maxVelocity.x = runSpeed+(heartRate /4);
			for(var i:int = 0; i < _animations.length; i++) {
				var anim:FlxAnim = (_animations[i] as FlxAnim);
				if (anim.name != "death") {
					(_animations[i] as FlxAnim).delay = 1 / (heartRate / 8);
				}
			}
		}
		
		public function die(fromLinePile:Boolean=false):void {
			heartRate = 0;
			alive = false;
			this.fromLinePile = fromLinePile;
			
			// make sure we stop moving
			velocity = new FlxPoint();
			acceleration = new FlxPoint();
			this.immovable = true;
				
			//play death animation
			play("death");
		}
		
		private function handleAnimationChanged(name:String, frameNumber:uint, frameIndex:uint):void
		{
			if (name == "death" && frameIndex == 13)
			{
				faceplantSound.play();
				if (fromLinePile && deathCallback != null)
				{
					fromLinePile = false;
					deathCallback();
				}
			}
		}
	}
}
